package com.nmsl.system.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.nmsl.system.domain.JinDepartmentsDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.nmsl.common.annotation.Log;
import com.nmsl.common.core.controller.BaseController;
import com.nmsl.common.core.domain.AjaxResult;
import com.nmsl.common.enums.BusinessType;
import com.nmsl.system.domain.JinDepartments;
import com.nmsl.system.service.IJinDepartmentsService;
import com.nmsl.common.utils.poi.ExcelUtil;

/**
 * 科室Controller
 * 
 * @author jin
 * @date 2025-02-19
 */
@RestController
@RequestMapping("/system/departments")
public class JinDepartmentsController extends BaseController
{
    @Autowired
    private IJinDepartmentsService jinDepartmentsService;

    /**
     * 查询科室列表
     */
    @PreAuthorize("@ss.hasPermi('system:departments:list')")
    @GetMapping("/list")
    public AjaxResult list(JinDepartments jinDepartments)
    {
        List<JinDepartments> list = jinDepartmentsService.selectJinDepartmentsList(jinDepartments);
        return success(list);
    }
    @GetMapping("/listTree")
    public AjaxResult listTree()
    {
        List<JinDepartments> list = jinDepartmentsService.selectJinDepartmentsTree();
        List<JinDepartmentsDTO> dtoList =list.stream().map(item->{
            JinDepartmentsDTO dto = new JinDepartmentsDTO();
            dto.setDisabled(false);
            dto.setId(item.getDepartmentId());
            dto.setLabel(item.getDepartmentName());
            dto.setChildren(item.getChildren());
            // 设置其他属性...
            return dto;
        }).collect(Collectors.toList());
        return success(dtoList);
    }
    /**
     * 导出科室列表
     */
    @PreAuthorize("@ss.hasPermi('system:departments:export')")
    @Log(title = "科室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JinDepartments jinDepartments)
    {
        List<JinDepartments> list = jinDepartmentsService.selectJinDepartmentsList(jinDepartments);
        ExcelUtil<JinDepartments> util = new ExcelUtil<JinDepartments>(JinDepartments.class);
        util.exportExcel(response, list, "科室数据");
    }

    /**
     * 获取科室详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:departments:query')")
    @GetMapping(value = "/{departmentId}")
    public AjaxResult getInfo(@PathVariable("departmentId") Long departmentId)
    {
        return success(jinDepartmentsService.selectJinDepartmentsByDepartmentId(departmentId));
    }

    /**
     * 新增科室
     */
    @PreAuthorize("@ss.hasPermi('system:departments:add')")
    @Log(title = "科室", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JinDepartments jinDepartments)
    {
        return toAjax(jinDepartmentsService.insertJinDepartments(jinDepartments));
    }

    /**
     * 修改科室
     */
    @PreAuthorize("@ss.hasPermi('system:departments:edit')")
    @Log(title = "科室", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JinDepartments jinDepartments)
    {
        return toAjax(jinDepartmentsService.updateJinDepartments(jinDepartments));
    }

    /**
     * 删除科室
     */
    @PreAuthorize("@ss.hasPermi('system:departments:remove')")
    @Log(title = "科室", businessType = BusinessType.DELETE)
	@DeleteMapping("/{departmentIds}")
    public AjaxResult remove(@PathVariable Long[] departmentIds)
    {
        return toAjax(jinDepartmentsService.deleteJinDepartmentsByDepartmentIds(departmentIds));
    }

}
